package com.keste.adf.ui.bean;

import com.keste.adf.ui.fwk.util.ADFUtils;

import com.keste.adf.ui.fwk.util.QuizUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCDataControl;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.ADFContext;
import oracle.adf.view.rich.component.rich.fragment.RichDeclarativeComponent;

import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;

import oracle.binding.BindingContainer;

import oracle.binding.OperationBinding;

import oracle.jbo.ApplicationModule;
import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.ViewObject;
import oracle.jbo.server.ViewObjectImpl;

public class QuestionADayBean {
    private Map ansOptions;
    private Map questions;
    private RichPanelGroupLayout questionComponent;

    public QuestionADayBean() {
 
        
 
        ansOptions = new HashMap();
        ansOptions.put("1", "Pradeep");
        ansOptions.put("2", "Anjani");
        
        questions = new HashMap();
        questions.put(1, "What is your name");
    }

    public void initQuestionADay()
    {
        return;
    }

    public void setAnsOptions(Map ansOptions) {
        this.ansOptions = ansOptions;
    }

    public Map getAnsOptions() {
        return ansOptions;
    }

    public void onSubmitQuestion(ActionEvent actionEvent) {
        System.out.println("in doSubmit");
        List answers = (List)ADFContext.getCurrent().getViewScope().get("SelectedAnswers");
        System.out.println(answers != null ? answers.size() : null);
    }

    public void setQuestions(Map questions) {
        this.questions = questions;
    }

    public Map getQuestions() {
        return questions;
    }

    public void onSubjectChange(ValueChangeEvent valueChangeEvent) {
        System.out.println("onSubjectChange:   " + valueChangeEvent.getNewValue());
        
        BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding dciter = (DCIteratorBinding)bindings.get("QuestionsVOIterator");
        ApplicationModule am= dciter.getViewObject().getApplicationModule();
        
        ViewObjectImpl vo = (ViewObjectImpl)am.findViewObject("QuestionsVO");
//        System.out.println("Before:  "+vo.getRowCount());
//        vo.executeQuery();        
//        vo.getCurrentRow().getAttribute("QuestionText");
        ViewObjectImpl subjectsVO =  (ViewObjectImpl)am.findViewObject("RefSubjectsVO");
        subjectsVO.setApplyViewCriteriaName("findSubjectByCode");
        subjectsVO.setNamedWhereClauseParam("Bind_SubjectCode", "SOA");
        subjectsVO.executeQuery();
        
        am.findViewObject("ExamsVO").executeQuery();
        am.findViewObject("ExamsVO").getCurrentRow().getAttribute("ExamCode");
        am.findViewObject("QuestionsInExamVO").executeQuery();
        am.findViewObject("QuestionsInExamVO").getRowCount();
        vo.executeQuery();
        
        System.out.println("Question: "+ vo.getCurrentRow());
        
        System.out.println("After:  "+vo.getRowCount());
        
        ////////
        subjectsVO.setApplyViewCriteriaName("findSubjectByCode");
        subjectsVO.setNamedWhereClauseParam("Bind_SubjectCode", "ADF");
        subjectsVO.executeQuery();
        
        am.findViewObject("ExamsVO").executeQuery();
        am.findViewObject("ExamsVO").getCurrentRow().getAttribute("ExamCode");
        am.findViewObject("QuestionsInExamVO").executeQuery();
        Row rw = am.findViewObject("QuestionsInExamVO").getCurrentRow();
        vo.executeQuery();
        
        System.out.println("Question: "+ vo.getCurrentRow());
        
        System.out.println("After:  "+vo.getRowCount());
    }

    public void OnSubjectSelection(ActionEvent actionEvent) {
        
//        ApplicationModule onlineQuizAM = QuizUtils.getApplicationModuleForDataControl("OnlineQuizAMDataControl");
//        ViewObjectImpl subjectsVO = (ViewObjectImpl)onlineQuizAM.findViewObject("RefSubjectsVO");
//        Key ky = new Key(new Object[]{QuizUtils.getPageFlowScopeMap().get("SubjectId")});
//        subjectsVO.setCurrentRow(subjectsVO.getRow(ky));
//        Object eL = QuizUtils.getEL("bindings.RefSubjectsVO.collectionModel.makeCurrent");

        String subjectId = QuizUtils.getPageFlowScopeMap().get("SubjectId").toString();
        Integer iSubjectId = Integer.parseInt(subjectId);

        OperationBinding qadMethod = ADFUtils.findOperationBinding("initializeQuestionADay");
        if (qadMethod != null) {
            qadMethod.getParamsMap().put("subjectId", iSubjectId);
            qadMethod.execute();
        }
        
        ADFUtils.addPartialTarget(this.getQuestionComponent());
        
//        ApplicationModule onlineQuizAM = QuizUtils.getApplicationModuleForDataControl("OnlineQuizAMDataControl");
//        ViewObjectImpl questionVO = (ViewObjectImpl)onlineQuizAM.findViewObject("QuestionADayVO");
//        ViewObjectImpl answersVO = (ViewObjectImpl)onlineQuizAM.findViewObject("QADAnswersVO");
//
//        System.out.println("Question:"+ questionVO.getCurrentRow().getAttribute("QuestionText"));
//        System.out.println("Answer 1:"+ answersVO.getRowAtRangeIndex(0).getAttribute("AnswerText"));
//        System.out.println("Answer 2:"+ answersVO.getRowAtRangeIndex(1).getAttribute("AnswerText"));
//        

        //        ViewObjectImpl examsVO = (ViewObjectImpl)onlineQuizAM.findViewObject("QADExamsVO");
//        examsVO.executeQuery();
//        ViewObjectImpl QuestionsinExamVO = (ViewObjectImpl)onlineQuizAM.findViewObject("QuestionsInQADExamVO");
//        QuestionsinExamVO.executeQuery();
//        
//        
//        System.out.println(QuestionsinExamVO.getRowCount());
    }

    public void setQuestionComponent(RichPanelGroupLayout questionComponent) {
        this.questionComponent = questionComponent;
    }

    public RichPanelGroupLayout getQuestionComponent() {
        return questionComponent;
    }
}
